'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟捐增
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var unionTechConf = require('../../../../config/unionTechConf.js');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionPositionType = require('../../../../consts/unionPositionType');
var PlayerManager = require('../../../../libs/playerManager');
var roleService = require('../../../../services/roleService.js');
var constantUtil = require('../../../../util/constantUtil.js');
var taskService = require('../../../../services/taskService.js');

/**
 * 联盟捐增
 */

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var donated_type = msg.donated_type;
    var tech_type = msg.tech_type;
    // jshint maxstatements:100
    // jshint maxdepth: 10
    var player_manager = new PlayerManager(role_id);
    var union_group_model;
    var union_id;
    var union_member_num;
    var reward_conf = null;
    var item_all = null;
    var donated_list = [];
    var tech_item = {};

    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                ['role', 'unionTask', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                union_id = role_model.getUnionId();
                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }

                if (!!union_id) {
                    unionGroupModel.getById(union_id, function (err, union_model) {
                        if (!!err) {
                            console.error('err------------union mgr not ', err);
                        }
                        union_group_model = union_model;
                        cb();
                    });
                } else {
                    cb();
                }
            },
            function (cb) {
                if (!!union_group_model) {
                    tech_item = union_group_model.getTechItem(tech_type);
                    var union_task_model = player_manager.unionTask;

                    // 是否已开启
                    if (!union_task_model.isDonated(tech_type, donated_type)) {
                        return next(null, {
                            code: Code.UNION_NOT_DONATED
                        });
                    }

                    item_all = union_task_model.isDonated(tech_type, donated_type);

                    var diff_time = constantUtil.value('union_tech_force_cdtime');
                    var now_time = moment().unix();
                    var tech_id_now = union_group_model.getTechId(tech_type);
                    var donated1_reward_type = unionTechConf.getTechConf(tech_id_now)[0].donation_type;

                    // 设置按钮1资源
                    union_task_model.setDonatedList(tech_type, 1, 1, donated1_reward_type, unionTechConf.getTechTypeConf(1));

                    if (donated_type == 1) {
                        // 随机
                        var conf2 = unionTechConf.getTechDonationConf(2);
                        var random_num2 = _.random(0, 100);
                        if (random_num2 <= 100) {
                            // if (random_num2 <= conf2.change_probability) {
                            var reward2 = unionTechConf.getDonated();
                            if (reward2.id == 1) {
                                union_task_model.setDonatedList(tech_type, 2, 1, item_all.reward_type, reward2.num);
                            } else {
                                union_task_model.setDonatedList(tech_type, 2, 1, reward2.id, reward2.num);
                            }
                        }

                        var conf3 = unionTechConf.getTechDonationConf(3);
                        var random_num3 = _.random(0, 100);
                        if (random_num3 <= conf3.change_probability) {
                            // if (random_num3 <= 100) {
                            var reward3 = unionTechConf.getDonated();

                            if (reward3.id == 1) {
                                union_task_model.setDonatedList(tech_type, 3, 1, item_all.reward_type, reward3.num);
                            } else {
                                union_task_model.setDonatedList(tech_type, 3, 1, reward3.id, reward3.num);
                            }
                        }

                        var donated_conf1 = unionTechConf.getTechDonationConf(1);

                        // 扣除资源
                        if (item_all.reward_type == 1) {
                            // 扣除金币
                            roleService.subGold(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        if (item_all.reward_type == 12) {
                            // 扣除subFood
                            roleService.subFood(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        if (item_all.reward_type == 2) {
                            // 扣除subDiamond
                            roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }

                        // 增加联盟资源
                        var conf1 = unionTechConf.getTechDonationConf(1);

                        // 增加科技经验
                        union_group_model.addTechExp(tech_type, conf1.tech_exp);

                        // 增加联盟经验
                        union_group_model.addUnionExp(conf1.union_exp);

                        // 增加声望
                        union_task_model.addTotalReputation(conf1.reputation);

                        // 是否在冷却时间
                        var diff_now_time = union_task_model.donated_time - now_time;
                        if (diff_now_time >= diff_time) {
                            return next(null, {
                                code: Code.UNION_NOT_TIME
                            });
                        }

                        // 增加冷却时间
                        union_task_model.addDonatedTime(conf1.add_time);
                    }

                    if (donated_type == 2) {
                        if (item_all.reward_type == 1) {
                            // 扣除金币
                            roleService.subGold(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        if (item_all.reward_type == 12) {
                            // 扣除subFood
                            roleService.subFood(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        if (item_all.reward_type == 2) {
                            // 扣除subDiamond
                            roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        // 重置联盟
                        union_task_model.setDonatedList(tech_type, 2, 0, 0, 0);

                        // 增加联盟资源
                        var gaim_conf2 = unionTechConf.getTechDonationConf(2);

                        // 增加科技经验
                        union_group_model.addTechExp(tech_type, gaim_conf2.tech_exp);

                        // 增加联盟经验
                        union_group_model.addUnionExp(gaim_conf2.union_exp);

                        // 增加声望
                        union_task_model.addTotalReputation(gaim_conf2.reputation);

                        // 是否在冷却时间
                        var diff_now_time2 = union_task_model.donated_time - now_time;
                        if (diff_now_time2 >= diff_time) {
                            return next(null, {
                                code: Code.UNION_NOT_TIME
                            });
                        }

                        // 增加冷却时间
                        union_task_model.addDonatedTime(gaim_conf2.add_time);
                    }

                    if (donated_type == 3) {
                        if (item_all.reward_type == 1) {
                            // 扣除金币
                            roleService.subGold(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        if (item_all.reward_type == 12) {
                            // 扣除subFood
                            roleService.subFood(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        if (item_all.reward_type == 2) {
                            // 扣除subDiamond
                            roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_EQUIP_FORGE, item_all.reward_num);
                        }
                        // 重置联盟
                        union_task_model.setDonatedList(tech_type, 3, 0, 0, 0);

                        // 增加联盟资源
                        var gaim_conf3 = unionTechConf.getTechDonationConf(3);

                        // 增加科技经验
                        union_group_model.addTechExp(tech_type, gaim_conf3.tech_exp);

                        // 增加联盟经验
                        union_group_model.addUnionExp(gaim_conf3.union_exp);

                        // 增加声望
                        union_task_model.addTotalReputation(gaim_conf3.reputation);

                        // 是否在冷却时间
                        var diff_now_time3 = union_task_model.donated_time - now_time;
                        if (diff_now_time3 >= diff_time) {
                            return next(null, {
                                code: Code.UNION_NOT_TIME
                            });
                        }

                        // 增加冷却时间
                        union_task_model.addDonatedTime(gaim_conf3.add_time);
                    }

                    // 增加任务次数统计
                    taskService.addUnionDonationNum(player_manager);
                    donated_list = union_task_model.donated_list;
                    cb();
                } else {
                    cb();
                }
            },
            function (cb) {
                player_manager.save(cb);
            },
            function (cb) {
                if (!!union_group_model) {
                    union_group_model.save(cb);
                } else {
                    cb();
                }
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    tech_item: tech_item
                }
            });
        }
    );
};
